<?php
global $wpdb;
global $table_shooters,$table_ranges;
if($_REQUEST['action'] == 'addshooter')
{
	$query = $wpdb->prepare("INSERT into $table_shooters (firstname,lastname,rangeid) values (%s,%s,%d)",
							$_REQUEST['shooters_firstname'],
							$_REQUEST['shooters_lastname'],
							$_REQUEST['shooters_rangeid']);
	$result = $wpdb->query($query);
	if($result === FALSE)
	{
		echo "Error: $wpdb->print_error()";
	}
}
else if($_REQUEST['action'] == 'changeshooter')
{
	$query = $wpdb->prepare("update $table_shooters set " .
							"firstname=%s, " .
							"lastname=%s, " . 
							"rangeid=%d " .
							"where $table_shooters.shooterid=%d",
							$_REQUEST['shooters_firstname'],
							$_REQUEST['shooters_lastname'],
							$_REQUEST['shooters_rangeid'],
							$_REQUEST['shooters_shooterid']);
	$result = $wpdb->query($query);
	if($result === FALSE)
	{
		echo "Error: $wpdb->print_error()";
	}
}
else if($_REQUEST['action'] == 'remove')
{
	$query = $wpdb->prepare("DELETE FROM $table_shooters WHERE $table_shooters.shooterid=%d",
							$_REQUEST['shooters_shooterid']);
	$result = $wpdb->query($query);
	if($result === FALSE)
	{
		echo "Error: $wpdb->print_error()";
	}
}
else if($_REQUEST['action'] == 'edit')
{
	$query = $wpdb->prepare("select $table_shooters.shooterid,$table_shooters.firstname, $table_shooters.lastname, $table_shooters.rangeid " . 
							 "from $table_shooters " .
							 "where $table_shooters.shooterid=%d",
							 $_REQUEST['shooters_shooterid']);
	$shooter = $wpdb->get_row($query);
	if($shooter === FALSE)
	{
		die($wpdb->print_error());
	}
	$ranges = $wpdb->get_results("select $table_ranges.rangeid, $table_ranges.name from $table_ranges");
	if($ranges === FALSE)
	{
		die($wpdb->print_error());
	}
?>
<form action="admin.php?page=<?php print $GLOBALS['ts_path']; ?>/<?php echo basename(__FILE__); ?>" method="post">
<div class="wrap">
<h2>Edit Shooter</h2>
<div>
<label for="firstname">Firstname:</label>
<input type="text" name="shooters_firstname" id="firstname" value="<?php echo $shooter->firstname ?>"  size="64" /><br />
<label for="lastname">Lastname:</label>
<input type="text" name="shooters_lastname" id="lastname" value="<?php echo $shooter->lastname ?>" size="64" /><p />
<label for="range">Member at:</label>
<select name="shooters_rangeid" id="range">
<?php
    foreach($ranges as $range)
    {
    	if($range->rangeid == $shooter->rangeid)
    		echo "<option selected value=" . $range->rangeid . ">" . $range->name . "</option>\n";
    	else
        	echo "<option value=" . $range->rangeid . ">" . $range->name . "</option>\n";
    }
?>
</select>
<input type="hidden" name="shooters_shooterid" value="<?php echo $shooter->shooterid ?>" />
<input type="hidden" name="action" value="changeshooter" />
</div>
<div class="submit"><input type="submit" value="Update Shooter &raquo;" /></div>
</div>
</form>
<?php
	return;
}
?>

<form action="admin.php?page=<?php print $GLOBALS['ts_path']; ?>/<?php echo basename(__FILE__); ?>" method="post">
<div class="wrap">
<h2>Current Shooters</h2>
<table>
<th width=30%>Name</th>
<th width=40%>Club</th>
<th colspan="3">Actions</th>
<?php
$shooters = $wpdb->get_results("select $table_shooters.shooterid, $table_shooters.firstname, $table_shooters.lastname, $table_ranges.name as rangename " . 
				      "from $table_shooters " . 
				      "join $table_ranges on $table_shooters.rangeid=$table_ranges.rangeid " . 
				      "order by $table_shooters.lastname asc, $table_shooters.firstname asc" .
				      "");
if ($shooters === FALSE) {
    die($wpdb->print_error());
}
foreach($shooters as $shooter)
{
    echo "<tr><td>" . $shooter->lastname . ", " . $shooter->firstname . "</td>\n";
    echo "<td>" . $shooter->rangename . "</td>\n";
    echo "<td><a href=\"admin.php?page=". $GLOBALS['ts_path'] . "/" . basename(__FILE__) . "&amp;shooters_shooterid=" . $shooter->shooterid . "&amp;action=edit\" class=\"edit\">Edit</a></td>";
    echo "<td><a href=\"admin.php?page=". $GLOBALS['ts_path'] . "/" . basename(__FILE__) . "&amp;shooters_shooterid=" . $shooter->shooterid . "&amp;action=remove\" class=\"delete\">Remove</a></td>";
    echo "<td><input type=\"checkbox\" name=\"shooters_shooterid[]\" value=\"" . $shooter->shooterid . "\"></td></tr>\n";
}
?>
</table>
<div class="submit">
<input type="submit" class="delete" name="action" value="Remove Selected Shooters" />
</div>
</div>
</form>

<form action="admin.php?page=<?php print $GLOBALS['ts_path'] ?>/<?php echo basename(__FILE__); ?>" method="post">
<div class="wrap">
<h2>Add a New Shooter:</h2>
<div>
<label for="firstname">Firstname:</label>
<input type="text" name="shooters_firstname" id="firstname" value="john" size="64" /><br />
<label for="lastname">Lastname:</label>
<input type="text" name="shooters_lastname" id="lastname" value="doe" size="64" /><p />
<label for="range">Member at:</label>
<select name="shooters_rangeid" id="range">
<?php
	$ranges = $wpdb->get_results("select $table_ranges.rangeid,$table_ranges.name from $table_ranges");
	if ($ranges === FALSE) {
      	        die($wpdb->print_error());
	}   
	foreach($ranges as $range)
	{
		echo "<option value=" . $range->rangeid . ">" . $range->name . "</option>\n";
	}
?>
</select>
<input type="hidden" name="action" value="addshooter" />
</div>
<div class="submit"><input type="submit" value="Add Shooter &raquo;" /></div>
</div>
</form>
